package com.example.demo_text;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import java.io.*;
import java.sql.*;

@WebServlet("/UploadVideoServlet")
public class UploadVideoServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        request.setCharacterEncoding("UTF-8");

        String courseId = request.getParameter("course_id");
        String chapterTitle = request.getParameter("chapter_title");
        String videoUrl = request.getParameter("video_url");

        try {
            VideoDAO.addVideo(courseId, chapterTitle, videoUrl);

            // ✅ 关键修改：跳转回 class_space.jsp 并激活 chapters 标签页
            response.sendRedirect("class_space.jsp?course_id=" + courseId + "&tab=chapters");

        } catch (Exception e) {
            e.printStackTrace();

            // 出错也返回 class_space.jsp 显示错误信息
            response.sendRedirect("class_space.jsp?course_id=" + courseId + "&error=视频上传失败：" + e.getMessage() + "#chapters");
        }
    }

}
